package com.veepeak.server.api.app;

import com.alibaba.fastjson.JSONObject;
import com.veepeak.server.common.JSONResult;
import com.veepeak.server.domain.DtcCommand;
import com.veepeak.server.domain.ModelCommand;
import com.veepeak.server.service.DtcCommandService;
import com.veepeak.server.service.ModelCommandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/command")
@Api(description = "OBD命令接口")
public class CommandApi {
    @Autowired
    private DtcCommandService dtcCommandService;
    @Autowired
    private ModelCommandService modelCommandService;

    @ApiOperation(value = "批量查询DTC故障码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pids", value = "pids", paramType = "body", required = true, dataTypeClass = String.class),
    })
    @PostMapping(value = "/dtc")
    public JSONObject getDTC(String pids){
        try {
            JSONObject data = new JSONObject();
            if ( pids.indexOf(",")>0) {
                String[] pidArray = pids.split(",");
                for (String pid : pidArray) {
                    DtcCommand dtc = dtcCommandService.getDtc(pid);
                    data.put(pid, JSONObject.toJSON(dtc));
                }
            }else{
                DtcCommand dtc = dtcCommandService.getDtc(pids);
                data.put(pids, JSONObject.toJSON(dtc));
            }
            return JSONResult.toSuccessJson(data,"");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.toFailureJson(e.getMessage());
        }
    }

    @ApiOperation(value = "查询Model命令", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "compIds", value = "compIds", paramType = "body", required = true, dataTypeClass = String.class),
    })
    @PostMapping(value = "/model")
    public JSONObject getModel(String compIds){
        try {

            JSONObject data = new JSONObject();
            if ( compIds.indexOf(",")>0) {
                String[] compIdArray = compIds.split(",");
                for (String compId : compIdArray) {
                    ModelCommand dtc = modelCommandService.getByCompId(compId);
                    data.put(compId, JSONObject.toJSON(dtc));
                }
            }else{
                ModelCommand dtc = modelCommandService.getByCompId(compIds);
                data.put(compIds, JSONObject.toJSON(dtc));
            }
            return JSONResult.toSuccessJson(data,"");
        } catch (Exception e) {
            return JSONResult.toFailureJson(e.getMessage());
        }
    }

    @ApiOperation(value = "查询Model命令", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mid", value = "mid", paramType = "path", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "tid", value = "tid", paramType = "path", required = true, dataTypeClass = String.class),
    })
    @GetMapping(value = "/model/{mid}/{tid}")
    public JSONObject getMidAndTid(@PathVariable String mid, @PathVariable String tid){
        try {
            ModelCommand modelCommand = modelCommandService.getByCompIdAndTestId(mid,tid);
            return JSONResult.toSuccessJson(modelCommand,"");
        } catch (Exception e) {
            return JSONResult.toFailureJson(e.getMessage());
        }
    }
}
